package com.ssy.bus.controller;

import com.ssy.bus.pojo.Purchase;
import com.ssy.bus.service.IPurchaseService;
import com.ssy.bus.service.ISupplierService;
import com.ssy.bus.vo.PurchaseVo;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author ssy
 * 入库审核维护
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private IPurchaseService purchaseService;

    @Autowired
    private ISupplierService supplierService;

    /**
     * 采购入库列表的分页
     *
     * @param purchaseVo 查询条件
     * @return data
     */
    @GetMapping("/loadAllPurchasePage")
    public PageResult loadAllPurchasePage(PurchaseVo purchaseVo) {
        return this.purchaseService.loadAllPurchasePage(purchaseVo);
    }

    /**
     * 加载供应商的查询下拉框
     *
     * @return data
     */
    @GetMapping("/loadAllSupplier")
    public Result loadAllSupplier() {
        return this.supplierService.selectAllSupplier();
    }

    /**
     * 暂存
     *
     * @param purchase purchase
     * @return data
     */
    @PostMapping("/addPurchase")
    public Result addPurchase(Purchase purchase) {
        purchase.setCreateTime(new Date());
        purchase.setWareTime(new Date());
        purchase.setState(2);
        purchase.setApplicant(purchase.getOperation());
        return this.purchaseService.InsertPurchase(purchase);
    }

    /**
     * 提交审核
     *
     * @param id 订单ID
     * @return data
     */
    @PostMapping("/submitExamine/{id}")
    public Result submitExamine(@PathVariable String id) {
        return this.purchaseService.updatePurchaseState(id);
    }

    /**
     * 批量提交审核
     *
     * @param ids    id
     * @param states stats
     * @return data
     */
    @PostMapping("/submitReview/{ids}/{states}")
    public Result submitReview(@PathVariable String[] ids, @PathVariable Integer[] states) {
        return this.purchaseService.submitPurchaseState(ids, states);
    }

    /**
     * 批量作废
     *
     * @param ids    id
     * @param states stats
     * @return data
     */
    @PostMapping("/toVoidPurchase/{ids}/{states}")
    public Result toVoidPurchase(@PathVariable String[] ids, @PathVariable Integer[] states) {
        return this.purchaseService.toVoidPurchaseState(ids, states);
    }

    /**
     * 入库审核的分页查询
     */
    @GetMapping("/loadAllPurchasePageByState")
    public PageResult loadAllPurchasePageByState(PurchaseVo purchaseVo) {
        return this.purchaseService.selectAllPurchasePageByState(purchaseVo);
    }

    /**
     * 审核成功
     */
    @PostMapping("/examineSuccess/{ids}")
    public Result examineSuccess(@PathVariable String[] ids) {
        Purchase purchase = new Purchase();
        purchase.setState(4);
        return this.purchaseService.updatePurchaseState(purchase, ids);
    }

    /**
     * 审核成功
     */
    @PostMapping("/examineFail/{ids}")
    public Result examineFail(@PathVariable String[] ids) {
        Purchase purchase = new Purchase();
        purchase.setState(5);
        return this.purchaseService.updatePurchaseState(purchase, ids);
    }

    /**
     * 提交入库
     *
     * @param ids    单据ID
     * @param states 状态码
     * @return data
     */
    @PostMapping("/submitPurchase/{ids}/{states}")
    public Result submitPurchase(@PathVariable String[] ids, @PathVariable Integer[] states) {
        return this.purchaseService.submitPurchase(ids, states);
    }



}
